package com.byzk.p2p.admin.city.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.city.dto.City;
import com.byzk.p2p.admin.city.dto.CityVo;
import com.byzk.p2p.admin.city.dto.Province;
import com.byzk.p2p.admin.city.service.CityService;

/**
 * 城市表
 * 
 * @author suker.zhou
 *
 */
@Controller
@RequestMapping("/city")
public class CityController extends AdminBaseController {

	@Autowired
	private CityService cityService;

	/**
	 * 城市查询
	 * 
	 * @param model
	 * @param city
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public void listUserAccount(Model model, @ModelAttribute City city) throws Exception {
		super.printJSON(new JsonMsg(-1, "查询成功！", cityService.listAll(city)));
	}
	
	/**
	 * 省级城市查询
	 * @param model
	 * @param tempCity
	 * @throws Exception
	 */
	@RequestMapping(value = "/temp/list")
	public void listTempCity(Model model, @RequestParam(value="province",required=false) String province) throws Exception {
		if(StringUtil.isBlank(province)){
			super.printJSON(new JsonMsg(-1, "查询成功！", cityService.getAllProvinces()));
		}else{
			super.printJSON(new JsonMsg(-1, "查询成功！", cityService.getTempCityByProvince(province)));
		}
	}
	
	/**
	 * 省级城市查询
	 * @param model
	 * @param tempCity
	 * @throws Exception
	 */
	/*@RequestMapping(value = "/getProvince")
	public void getProvince() throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("hot", null);
		data.put("province", cityService.getProvince());
		super.printJSON(new JsonMsg(-1, "查询成功！",data));
	}*/
	
	@RequestMapping("/toJson")
	public String cityToJson() throws Exception{
		List<Province> json=new ArrayList<Province>();
		
		List<City> cityAll=cityService.getJsonCity(-1);
		for (City city : cityAll) {
			Province province=new Province();
			province.setId(city.getCode());
			province.setName(city.getName());
			List<City> tempList=cityService.getJsonCity(city.getId());
			List<CityVo> voList=new ArrayList<CityVo>();
			for (City temp : tempList) {
				CityVo vo=new CityVo();
				vo.setId(temp.getCode());
				vo.setName(temp.getName());
				voList.add(vo);
			}
			province.setCity(voList);
			json.add(province);
		}
		System.out.println(JSON.toJSONString(json));
		super.printJSON(new JsonMsg(-1, "OK"));
		return null;
	}
}

